/**
 * @Author yinyunqi
 * @date 2017年12月23日
 * @Content 成员信息控制器
 */
package com.yongqi.controller.console;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.Member;
import com.yongqi.pub.DateFormat;
import com.yongqi.service.MemberService;

@Controller 
@RequestMapping("/member")
public class MemberController {
    @Autowired
    MemberService memberService;
    
    /**
     * 成员管理中添加成员功能
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/memberAdd")
    public String memberAdd(HttpServletRequest request) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Member member = new Member();
	member.setFamilyAccount(familyAccount);
	member.setMemberAccount(request.getParameter("memberAccount"));
	member.setMemberName(request.getParameter("memberName"));
	DateFormat DateFormat = new DateFormat();
	member.setCreated_time(DateFormat.dateFormate());
	memberService.memberAdd(member);
	JSONObject object = new JSONObject();
	object.put("result", "success");
	return object.toJSONString();
    }
    /**
     * 跳转到成员首页
     * @return
     */
    @RequestMapping("/index")
    public String memberIndex() {
	return "member/MemberIndex";
    }
    /**
     * 添加成员信息
     * @return
     */
    @RequestMapping("/add")
    public String memberAdd() {
	return "member/MemberAdd";
    }
    /**
     * 获取分页的成员信息
     * @return 
     */
    @ResponseBody
    @RequestMapping("/query")
    public String  memberQuery(@RequestParam int pageNumber,int pageSize ) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", familyAccount);
	int total = memberService.memberQuery(map).size();
	PageHelper.startPage(pageNumber,pageSize);
	List<Member> memberList = memberService.memberQuery(map);
	JSONObject object = new JSONObject();
	object.put("rows", memberList);
	object.put("total", total);
	return object.toJSONString();
    }
    @ResponseBody
    @RequestMapping("/delete")
    public String memberDelete(HttpServletRequest request) {
	String[] ids = request.getParameterValues("ids");
	for(int i=0;i<ids.length;i++) {
	    memberService.memberDelete(ids[i]);
	}
	JSONObject object = new JSONObject();
	object.put("result", "success");
	return object.toJSONString();
    }
}

